package com.bblocks.auth.pojo;

import com.bblocks.enums.SysEnum;
import com.bblocks.util.R;
import com.bblocks.util.Result;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Data
@Schema(description="登录响应（2个数据对象）")
public class ResultLogin<T> implements Serializable {
	private static final long serialVersionUID = 1L;

	//响应状态
	@Schema(description="响应码 1=成功，其他为失败")
	protected Integer result;

	@Schema(description="响应码 和code一致")
	private Integer code;

	//响应消息
	@Schema(description="消息/错误消息")
	protected String message;

	//响应数据
	@Schema(description="数据")
	protected T object;
	//响应数据
	@Schema(description="数据（和object一致）")
	private T data;


	public ResultLogin(SysEnum sysEnum) {
		this.result = sysEnum.getCode();
		this.message = sysEnum.getMessage();

		this.code = this.result;
	}


	public ResultLogin(Integer result, String message) {
		this.result = result;
		this.message = message;

		this.code = this.result;
	}

	public ResultLogin(SysEnum sysEnum, T object) {
		this.result = sysEnum.getCode();
		this.message = sysEnum.getMessage();

		this.object = object;
		this.data = object;

		this.code = this.result;
	}

	public ResultLogin(Integer result, String message, T object) {
		this.result = result;
		this.message = message;
		this.object = object;

		this.data = object;

		this.code = this.result;
	}


	/**
	 * 系统异常
	 *
	 * @return
	 */
	public static <T> ResultLogin<T> SysError() {
		return new ResultLogin(0, "系统异常");
	}

	public static <T> ResultLogin<T> SysError(SysEnum sysEnum) {
		return new ResultLogin(sysEnum.getCode(), sysEnum.getMessage());
	}

	public static <T> ResultLogin<T> SysError(String message) {
		return new ResultLogin(0, message);
	}


	public void setResult(SysEnum sysEnum, T object) {
		this.result = sysEnum.getCode();
		this.message = sysEnum.getMessage();
		this.object = object;
		this.data = object;


		this.code = this.result;
	}

	public void setResult(SysEnum sysEnum) {
		this.result = sysEnum.getCode();
		this.message = sysEnum.getMessage();

		this.code = this.result;
	}


	/**
	 * 参数验证失败返回结果
	 * @param message 提示信息
	 */
	public static <T> ResultLogin validateFailed(String message) {
		return new ResultLogin(SysEnum.PARAMS_IS_NULL, message);
	}

	/**
	 * 参数验证失败返回结果
	 * @param message 提示信息
	 */
	public static <T> ResultLogin validateFailed() {
		return new ResultLogin(SysEnum.PARAMS_IS_NULL);
	}

	public static <T> ResultLogin<T> ok() {
		return restResult(null, SysEnum.SUCCESS.getCode(), SysEnum.SUCCESS.getMessage());
	}
	public static <T> ResultLogin<T> ok(String msg) {
		return restResult(null, SysEnum.SUCCESS.getCode(), msg);
	}
	public static <T> ResultLogin<T> okData(T data) {
		return restResult(data, SysEnum.SUCCESS.getCode(), SysEnum.SUCCESS.getMessage());
	}

	public static <T> ResultLogin<T> failed(int code, String msg) {
		return restResult(null, code, msg);
	}

	private static <T> ResultLogin<T> restResult(T data, Integer result, String msg) {
		ResultLogin<T> apiResult = new ResultLogin<>(result,msg,data);
		return apiResult;
	}
}
